import { EventHandler, IValid, IAppEvent, IEventDispatcher } from "../../interfaces";
import { EventEmitter } from "../../event/EventEmitter";
import { EventDispatcher } from "../../event/EventDispatcher";
import { IModel } from "../view/IModel";


export class Model<C = any, D = any> extends EventEmitter implements IModel {
    //用于将事件发送给自己的view或者controller；不需要让其他模块知道。
    protected event: IEventDispatcher = new EventDispatcher();
    private _cfg: C;
    private _db: D;
    private _state: number = 0;

    setState(s: number): void {
        this._state = s;
    }

    getState(): number {
        return this._state;
    }

    setCfg(cfg: C): void {
        this._cfg = cfg;
    }

    getCfg(): C {
        return this._cfg;
    }

    setDB(d: D): void {
        this._db = d;
    }

    getDB(): D {
        return this._db
    }



    on(topic: string, handler: EventHandler, target: IValid): void {
        this.event.on(topic, handler, target)
    }


    off(topic: string, handler: EventHandler, target: IValid): void {
        this.event.off(topic, handler, target)
    }

    emit(topic: string, data: any): void {
        this.event.emit(topic, data)
    }

    init() {

    }
}